package com.speech.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ontbee.legacyforks.cn.pedant.SweetAlert.SweetAlertDialog;
import com.speech.AppConfig;
import com.speech.R;
import com.speech.activity.CoinDetailActivity;
import com.speech.activity.EditMineEntranceActivity;
import com.speech.activity.FeedbackActivity;
import com.speech.activity.IntroduceActivity;
import com.speech.activity.SettingActivity;
import com.speech.utils.PropertyAndUserInfoHolder;
import com.speech.utils.StringFomatUtil;

public class MineFragment extends Fragment {

    private ImageView img_edit;
    private TextView tv_edit;
    private TextView tv_spk2cash;
    private TextView tv_coin_num;
    private TextView tv_nickname;

    private SimpleDraweeView sdv_mine_head;

    private ConstraintLayout cl_coinDetail;
    private ConstraintLayout cl_feedback;
    private ConstraintLayout cl_setting;

    private View.OnClickListener listener_edit;

    private ImageView img_introduce;

    private boolean isCreated = false;

    private SweetAlertDialog dialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null);
        isCreated = true;
        _initView(view);

        tv_nickname.setText(PropertyAndUserInfoHolder.getStrNickName(getActivity()));
        tv_coin_num.setText(StringFomatUtil.formatDouble(PropertyAndUserInfoHolder.getUserTotalProperty()));

        if (AppConfig.URI_HEAD_PICTURE != null) {
            sdv_mine_head.setImageURI(AppConfig.URI_HEAD_PICTURE);
        }


        img_edit.setOnClickListener(_get_edit_listener());
        tv_edit.setOnClickListener(_get_edit_listener());

        tv_spk2cash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("钱包功能即将上线，敬请期待！")
                        .setConfirmText("OK");
                        dialog.show();
            }
        });

        // 资产明细
        cl_coinDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CoinDetailActivity.class));
            }
        });

        // 意见反馈
        cl_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
            }
        });

        // 设置
        cl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });

        // 介绍
        img_introduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), IntroduceActivity.class));
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (PropertyAndUserInfoHolder.getPropertyObject() != null && PropertyAndUserInfoHolder.getPropertyObject().dTotalProperty != null) {
            tv_coin_num.setText(StringFomatUtil.formatDouble(PropertyAndUserInfoHolder.getPropertyObject().dTotalProperty));
        }

        tv_nickname.setText(PropertyAndUserInfoHolder.getStrNickName(getActivity()));

        if (AppConfig.URI_HEAD_PICTURE != null) {
            sdv_mine_head.setImageURI(AppConfig.URI_HEAD_PICTURE);
        }
    }

    private void _initView(View view) {
        img_edit = view.findViewById(R.id.img_mine_edit_pen);
        sdv_mine_head = view.findViewById(R.id.sdv_mine_head);

        tv_edit = view.findViewById(R.id.tv_mine_edit);
        tv_coin_num = view.findViewById(R.id.tv_mine_coin_num);

        tv_nickname = view.findViewById(R.id.tv_mine_userName);

        tv_spk2cash = view.findViewById(R.id.tv_mine_spk_to_cash);

        cl_coinDetail = view.findViewById(R.id.cl_mine_coinDetail);
        cl_feedback = view.findViewById(R.id.cl_mine_feedback);
        cl_setting = view.findViewById(R.id.cl_mine_setting);

        img_introduce = view.findViewById(R.id.img_mine_introduce);
    }

    private View.OnClickListener _get_edit_listener() {

        if (listener_edit == null) {
            listener_edit = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), EditMineEntranceActivity.class));
                }
            };
        }

        return listener_edit;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isCreated) {
            return;
        }

        if (isVisibleToUser) {
            tv_nickname.setText(PropertyAndUserInfoHolder.getStrNickName(getActivity()));
            tv_coin_num.setText(StringFomatUtil.formatDouble(PropertyAndUserInfoHolder.getUserTotalProperty()));

            if (AppConfig.URI_HEAD_PICTURE != null) {
                sdv_mine_head.setImageURI(AppConfig.URI_HEAD_PICTURE);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dialog != null){
            dialog.dismissWithAnimation();
        }
    }
}
